<template>
  <h2>Home</h2>

  <el-upload
    ref="upload"
    class="upload-demo"
    action="http://localhost:8000/api/test/"
    :auto-upload="false"
    :limit="1"
    :multiple="false"
    :on-exceed="handleExceed"
  >
    <template #trigger>
      <el-button type="primary">select file</el-button>
    </template>

    <el-button class="ml-3" type="success" @click="submitUpload">
      upload to server
    </el-button>

    <template #tip>
      <div class="el-upload__tip">jpg/png files with a size less than 500kb</div>
    </template>
  </el-upload>
</template>

<script setup>
import { ref } from "vue";
import { genFileId } from 'element-plus'
const upload = ref();

const submitUpload = () => {
  upload.value.submit();
};

const handleExceed= (files) => {
  upload.value.clearFiles()
  const file = files[0] 
  file.uid = genFileId()
  upload.value.handleStart(file)
}


</script>
